<template>
  <el-dialog
    :title="title"
    :visible="dialogVisible"
    width="50%"
    @close="handleDialog"
    @open="handleOpen"
  >

    <!-- 匿名插槽 -->
    <el-form ref="addForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in people"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="submitData">确定</el-button>
      <el-button size="small" @click="handleDialog">取消</el-button>
    </div></el-dialog>
</template>

<script>
import { reqAddDepots, reqGetDepotInfo, reqChangeDportments } from '@/api/departments'
import { reqGetEmployInfo } from '@/api/employees'
export default {
  name: 'AddList',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
      required: true
    },
    datalist: {
      type: Object,
      required: true
    },
    list: {
      type: Array,
      required: true
    }
  },
  data() {
    // 部门名称重复
    const checkname = (rule, value, callback) => {
      let arr = []
      if (this.form.id) {
        if (this.datalist.name === value) {
          callback()
          return
        }
        arr = this.list.filter(item => item.pid === this.datalist.pid)
      } else {
        arr = this.list.filter(item => item.pid === this.datalist.id)
      }
      const name = arr.some(item => item.name === value)
      name ? callback(new Error('部门名称重复')) : callback()
    }

    // 部门编码重复
    const checkCode = (rule, value, callback) => {
      if (this.form.id && value === this.datalist.code) {
        callback()
        return
      }
      const code = this.list.some(item => item.code === value)
      code ? callback(new Error('部门编码重复')) : callback()
    }
    return {
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 2, max: 10, message: '部门名称格式错误', trigger: ['blur', 'change'] },
          { validator: checkname, trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码格式错误', trigger: ['blur', 'change'] },
          { validator: checkCode, trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }

        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 30, message: '部门介绍格式错误', trigger: ['blur', 'change'] }
        ]
      },
      people: {}
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑部门' : '添加部门'
    }
  },
  methods: {
    // 打开/关闭弹窗
    handleDialog() {
      // this.$emit('closeDialog')
      this.$emit('update:dialogVisible', false)
      this.$refs.addForm.resetFields()
      this.form = {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      }
    },
    async handleOpen() {
      const { data: res } = await reqGetEmployInfo()
      this.people = res
    },
    submitData() {
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        if (this.form.id) {
          await reqChangeDportments(this.form)
          this.$message.success('修改部门成功')
        } else {
          await reqAddDepots({ ...this.form, pid: this.datalist.id })
          this.$message.success('添加部门成功')
        }
        this.handleDialog()
        this.$emit('addDepots')
      })
    },
    async getDepartDetail() {
      const { data } = await reqGetDepotInfo(this.datalist.id)
      this.form = data
    }

  }
}
</script>

<style>

</style>
